package com.cj.web.back;

import com.cj.entity.PageBean;
import com.cj.entity.User;
import com.cj.service.UserService;
import com.cj.utils.JSONUtils;
import com.cj.utils.ResultVO;
import com.cj.web.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    ResultVO vo;
    UserService service =  new UserService();
// http://localhost/education/user?method=login
    public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("login");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");

        HttpSession session = req.getSession();

        // 谷歌浏览器不允许跨域携带cookie
        String verCode = (String) session.getAttribute("verCode");

        System.out.println(verCode);
//        验证码相等再做service
        if (code.equalsIgnoreCase("1234")){
//
            User user = service.checkLogin(username,password);
            if (user != null) {
//                先判断他是否禁用，再判断他是否有权限
                if (user.getStatus() == 2) {//2 表示禁用  1表示可用
                     vo = new ResultVO(500, "用户被禁用", null);
                }else{
                    if (user.getRole()!=1){
                        vo=new ResultVO(500,"权限不足",null);
                    }else{
//                        存入session域，整个会话都可以使用
                        session.setAttribute("user",user);
                        vo=new ResultVO(200,null,user);
                    }
                }
            }else{
                vo = new ResultVO(500,"用户名或密码错误",null);
            }
        }else{
             vo = new ResultVO(500, "验证码错误", null);
        }

        JSONUtils.objToJson(vo,resp);

    }

    public void findUser(HttpServletRequest req,HttpServletResponse resp) throws IOException {
//
        User user = (User) (req.getSession().getAttribute("user"));
//        存在user 找不到的问题
        if (user != null) {
             vo = new ResultVO(200, "获取用户信息成功", user);
        }else{
            vo = new ResultVO(500,"获取用户信息失败",null);
        }

//  obj是数据    使用ObjectMapper转换为json的时候，必须要给字段写 set get 方法
        JSONUtils.objToJson(vo,resp);
    }

    public void loginOut(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        req.getSession().invalidate();
         vo = new ResultVO(200, "成功退出", null);
        JSONUtils.objToJson(vo,resp);
    }

// 查找当前页面
    public void findByPage(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String name = req.getParameter("name");

       PageBean<User> pb =  service.findByPage(currentPage,pageSize,name);

       JSONUtils.objToJson(pb,resp);
    }

//    新增用户
    public void insertUser(HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        User user = new User();
        BeanUtils.populate(user,map);

        int row = service.insertUser(user);

        if(row>0){
            vo = new ResultVO(200,"添加成功",null);
        }else{
            vo = new ResultVO(500,"添加失败",null);
        }
        JSONUtils.objToJson(vo,resp);
    }


    public void deleteUser(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String uids = req.getParameter("uids");

        int row = service.deleteUser(uids);

        if(row>0){
            vo = new ResultVO(200,"批量删除用户成功",null);
        }else{
            vo = new ResultVO(500,"批量删除用户失败",null);
        }
        JSONUtils.objToJson(vo,resp);
    }


//    修改用户
    public void updateUser(HttpServletRequest req,HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        User user = new User();
        BeanUtils.populate(user,map);

        int row = service.updateUser(user);

        if(row > 0 ){
            vo = new ResultVO(200,"修改信息成功",null);
        }else{
            vo = new ResultVO(500,"修改信息失败",null);
        }

        JSONUtils.objToJson(vo,resp);
    }

}
